package com.ssm.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.ssm.base.BaseController;
import com.ssm.entity.UserInfo;
import com.ssm.result.Result;
import com.ssm.service.UserFollowService;
import com.ssm.vo.UserFollowVo;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author jie_ge
 * @Date 2022/4/21
 */
@RestController
@RequestMapping("/userFollow")
@CrossOrigin
public class UserFollowController extends BaseController {
    @Reference
    private UserFollowService userFollowService;
    /**
     * 关注房源  关注房源是一定要登录的，定义url规则：凡是url中包含auth的都需要登录处理
     * @param houseId
     * @param request
     * @return
     */
    @GetMapping("/auth/follow/{houseId}")
    public Result follow(@PathVariable("houseId") Long houseId, HttpServletRequest request){
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute("USER");
        Long userId = userInfo.getId();
        userFollowService.follow(userId,houseId);
        return Result.ok();
    }

    /**
     * 我的关注
     * @param pageNum
     * @param pageSize
     * @param request
     * @return
     */
    @GetMapping("/auth/list/{pageNum}/{pageSize}")
    public Result findListPage(@PathVariable("pageNum") Integer pageNum,@PathVariable("pageSize") Integer pageSize,HttpServletRequest request){
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute("USER");
        Long userId = userInfo.getId();
        PageInfo<UserFollowVo> pageInfo = userFollowService.findListPage(pageNum, pageSize, userId);
        return Result.ok(pageInfo);
    }

    /**
     * 取消关注
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/auth/cancelFollow/{id}")
    public Result cancelFollow(@PathVariable("id") Long id,HttpServletRequest request){
        userFollowService.cancelFollow(id);
        return Result.ok();
    }
}
